package com.ninthgroup.controller;

import cn.dsna.util.images.ValidateCode;

import com.ninthgroup.vo.Result;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author ：1452863884@qq.com rcg
 * @date ：Created in 2022/11/2 17:35
 * @description：图片验证码控制层
 * @modified By：
 * @version:1.0
 */
@RestController
@RequestMapping("/code")
@Slf4j
public class ValidateCodeController {

    private static String CODE_AREA=null;

    @GetMapping("/getValidateCode")
    public void getValidateCode(HttpServletResponse response, HttpSession session) throws IOException {
        //参数分别为验证码图片长、高，字符数、干扰线数
        ValidateCode validateCode = new ValidateCode(200, 40, 4, 50);
        validateCode.write(response.getOutputStream());
        //获取生成的验证码
        String validateCodeCode = validateCode.getCode();
        log.info("the validate code is:" + validateCodeCode);
        CODE_AREA=validateCodeCode;
        session.setAttribute("validateCode", validateCodeCode);
    }

    @GetMapping("/outcode")
    public Result outValidateCode(){

        return new Result(200,"输出验证码成功",CODE_AREA);
    }

}